package top.wardyan.wbaseswt.control.wtable;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import com.jfinal.aop.Aop;

//import top.wardyan.conf.Const;
//import top.wardyan.conf.config.SQLite;
//import top.wardyan.conf.model.YTTableCol;
//import top.wardyan.util.ShellUtil;
import top.wardyan.wbaseswt.action.base.AbstractShell;
import top.wardyan.wbaseswt.conf.Const;
import top.wardyan.wbaseswt.conf.model.SysTableCol;
import top.wardyan.wbaseswt.conf.service.ISysTableColService;
import top.wardyan.wbaseswt.control.WComposite;
import top.wardyan.wbaseswt.control.WLabel;
import top.wardyan.wbaseswt.control.WText;
import top.wardyan.wbaseswt.util.ShellUtil;

public class WTableColumnSettingShell extends AbstractShell<WTableColumnSettingShell>{

	protected String classname;
	protected String tablename;
	
	private ISysTableColService tableColService = Aop.get(ISysTableColService.class);
	
	public WTableColumnSettingShell() {
		this("top.wardyan.wbaseswt.control.wtable.WTableColumnSettingShell", "mainTable");
	}
	
	public WTableColumnSettingShell(String classname, String tablename) {
		super();
		this.classname = classname;
		this.tablename = tablename;
		
		this.createContent();
	}
	
	protected WText searchText;
	
	private void createContent() {
		this.setSize(800, 600);
		ShellUtil.center(this);
		this.setLayout(new FormLayout());
		
		/** 测试 **/
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// super.mouseDoubleClick(e);
				self.dispose();
				new WTableColumnSettingShell().go();;
			}
		});
	
		
		searchText = new WText(this, SWT.BORDER, "", "搜索关键字");
		searchText.setTextLimit(64);
		FormData fd_searchText = new FormData();
		fd_searchText.bottom = new FormAttachment(this, 30);
		fd_searchText.left = new FormAttachment(this, 0);
		fd_searchText.right = new FormAttachment(100, -1);
		fd_searchText.top = new FormAttachment(this, 2);
		searchText.setLayoutData(fd_searchText);
		
		WComposite mainCom = new WComposite(this, SWT.NONE);
		FormData fd_mainCom = new FormData();
		fd_mainCom.bottom = new FormAttachment(100, -1);
		fd_mainCom.left = new FormAttachment(this, 0);
		fd_mainCom.right = new FormAttachment(100, -1);
		fd_mainCom.top = new FormAttachment(searchText, 6);
		mainCom.setLayoutData(fd_mainCom);
		mainCom.setLayout(new FormLayout());
		
		List<SysTableCol> cols = tableColService.list(classname, tablename);
		if(cols != null) {
			WLabel indexLabel = new WLabel(mainCom, SWT.BORDER, Const.FONT12, "序号", SWT.CENTER);
			FormData fd_indexLabel = new FormData();
			fd_indexLabel.bottom = new FormAttachment(mainCom, 35);
			fd_indexLabel.left = new FormAttachment(this, 0);
			fd_indexLabel.right = new FormAttachment(10, 0);
			fd_indexLabel.top = new FormAttachment(mainCom, 6);
			indexLabel.setLayoutData(fd_indexLabel);
			WLabel fieldLabel = new WLabel(mainCom, SWT.BORDER, Const.FONT12, "字段", SWT.CENTER);
			FormData fd_fieldLabel = new FormData();
			fd_fieldLabel.bottom = new FormAttachment(mainCom, 35);
			fd_fieldLabel.left = new FormAttachment(indexLabel, 0);
			fd_fieldLabel.right = new FormAttachment(45, 0);
			fd_fieldLabel.top = new FormAttachment(mainCom, 6);
			fieldLabel.setLayoutData(fd_fieldLabel);
			WLabel nameLabel = new WLabel(mainCom, SWT.BORDER, Const.FONT12, "名称", SWT.CENTER);
			FormData fd_nameLabel = new FormData();
			fd_nameLabel.bottom = new FormAttachment(mainCom, 35);
			fd_nameLabel.left = new FormAttachment(fieldLabel, 0);
			fd_nameLabel.right = new FormAttachment(90, 0);
			fd_nameLabel.top = new FormAttachment(mainCom, 6);
			nameLabel.setLayoutData(fd_nameLabel);
			WLabel widthLabel = new WLabel(mainCom, SWT.BORDER, Const.FONT12, "宽度", SWT.CENTER);
			FormData fd_widthLabel = new FormData();
			fd_widthLabel.bottom = new FormAttachment(mainCom, 35);
			fd_widthLabel.left = new FormAttachment(nameLabel, 0);
			fd_widthLabel.right = new FormAttachment(100, -1);
			fd_widthLabel.top = new FormAttachment(mainCom, 6);
			widthLabel.setLayoutData(fd_widthLabel);
			
			WComposite pre = null;
			for(SysTableCol c : cols) {
				WComposite com = new WComposite(mainCom, SWT.NONE);
				FormData fd_com = new FormData();
				fd_com.bottom = new FormAttachment(com, 30);
				fd_com.left = new FormAttachment(this, 0);
				fd_com.right = new FormAttachment(100, 0);
				fd_com.top = new FormAttachment(pre != null ? pre : indexLabel, 0);
				com.setLayoutData(fd_com);
				com.setLayout(new FormLayout());
				pre = com;
				//
				WText sID = new WText(com, SWT.BORDER, String.valueOf(c.getSID()), "sID");
				sID.setVisible(false);
				WText index = new WText(com, SWT.BORDER, String.valueOf(c.getSID()), "序号");
				FormData fd_index = new FormData();
				fd_index.bottom = new FormAttachment(index, 30);
				fd_index.left = new FormAttachment(com, 0);
				fd_index.right = new FormAttachment(10, 0);
				fd_index.top = new FormAttachment(com, 0);
				index.setLayoutData(fd_index);
				WText field = new WText(com, SWT.BORDER, c.getXField(), "字段");
				FormData fd_field = new FormData();
				fd_field.bottom = new FormAttachment(field, 30);
				fd_field.left = new FormAttachment(index, 0);
				fd_field.right = new FormAttachment(45, 0);
				fd_field.top = new FormAttachment(com, 0);
				field.setLayoutData(fd_field);
				WText name = new WText(com, SWT.BORDER, c.getXTitle(), "名称");
				FormData fd_name = new FormData();
				fd_name.bottom = new FormAttachment(name, 30);
				fd_name.left = new FormAttachment(field, 0);
				fd_name.right = new FormAttachment(90, 0);
				fd_name.top = new FormAttachment(com, 0);
				name.setLayoutData(fd_name);
				WText width = new WText(com, SWT.BORDER, String.valueOf(c.getXWidth()), "宽度");
				FormData fd_width = new FormData();
				fd_width.bottom = new FormAttachment(width, 30);
				fd_width.left = new FormAttachment(name, 0);
				fd_width.right = new FormAttachment(100, -1);
				fd_width.top = new FormAttachment(com, 0);
				width.setLayoutData(fd_width);
			}
			
			// btn save reset
		}
		
	}
	
	
	@Override
	protected String getTitle() {
		return "列设置";
	}
	
	public static void main(String[] args) {
		new WTableColumnSettingShell().go();
	}

}
